<template>
    <div style="width: 100%; height: 100%;">


        <!--地图容器-->
        <div id="sceneContainer"></div>

        <!--初始化出错提示-->
        <el-dialog
                title="警告"
                v-model="dialogVisible"
                width="50%"
                destroy-on-close
                :close-on-click-modal="false"
                center>
            <span>图层配置文件加载失败，请检查网络及配置文件。</span>
            <strong>[初始化失败，初始化数据失败。]</strong>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="danger" @click="dialogVisible = false">确 定</el-button>
                </span>
            </template>

        </el-dialog>

    </div>
</template>

<script>

import ToolsMenu from "../ToolsMenu/ToolsMenu";
import store from "@/store/store";
import ImageryLayerModel from "@/DataManager/imple/ImageryLayerModel";
import LoadingLayers from "./LoadingLayers";
import S3MLayerModel from "../../DataManager/imple/S3MLayerModel";

let layers_init;

export default {
  name: "SmViewer",
  components: { ToolsMenu },
  data(){
    return {
      sharedState: store.state,
      // 是否显示对话框
      dialogVisible: false
    };
  },
  mounted(){
    // 初始化地图
    this.initViewer();
  },
  methods: {
    async initViewer(){

      layers_init = await axios.get("./js/CustConfig/init/layers_init.json");
      // console.log(`layers_init`, layers_init);

      // 实例化地图容器，直接挂载到Window中
      window.viewer = new window.Cesium.Viewer("sceneContainer");
      window.scene = viewer.scene;

      // 是否成功加载配置文件
      if (!layers_init) {
        this.dialogVisible = true;
        return;
      }
      if (this.sharedState.debug) {
        this.sharedState.state.viewerLayers = {};
        ImageryLayerModel.removeAll();
        S3MLayerModel.removeAll();
      }

      /***初始化加载的图层 **start*******************************************************************************************************************************/
      // 天地加载配置
      if (layers_init.tianditu) {
        LoadingLayers.addTianditu(layers_init.tianditu);
      }

      // 其他初始化加载的图层。为了处理图层列表中排序的问题，初始化加载的图层不会显示在图层列表中。
      if (layers_init.viewer_init_layers) {
        LoadingLayers.andAddLayers(layers_init.viewer_init_layers);
      }
      /***初始化加载的图层 **end*******************************************************************************************************************************/

      // console.log(`Viewer初始化完成`);

      // 加载图层配置文件
      if (layers_init.layers) {
        // 解析配置文件并加载图层
        LoadingLayers.analysisAndAddLayers(layers_init.layers);
      }
      // console.log(`加载图层完成`);
    }
  }
};
</script>

<style scoped>
    /*修改地图的样式，占满整个容器*/
    #sceneContainer {
        width: 100%;
        height: 100%;
    }

</style>
